package io.ede.modules.ede.controller;

import java.util.*;

import io.ede.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.ede.modules.ede.entity.AreaEntity;
import io.ede.modules.ede.service.AreaService;
import io.ede.common.utils.PageUtils;
import io.ede.common.utils.R;


/**
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-09-12 16:13:57
 */
@RestController
@RequestMapping("ede/area")
public class AreaController {
    @Autowired
    private AreaService areaService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ede:area:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = areaService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ede:area:info")
    public R info(@PathVariable("id") Integer id) {
        AreaEntity area = areaService.getById(id);

        return R.ok().put("area", area);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ede:area:save")
    public R save(@RequestBody AreaEntity area) {
        area.setDelFlag("1");
        area.setCreateTime(new Date());
        areaService.save(area);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ede:area:update")
    public R update(@RequestBody AreaEntity area) {
        ValidatorUtils.validateEntity(area);
        area.setUpdateTime(new Date());
        areaService.updateById(area);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ede:area:delete")
    public R delete(@RequestBody Integer[] ids) {
        AreaEntity area = new AreaEntity();
        area.setDelFlag("0");
        area.setUpdateTime(new Date());
        for (int i = 0; i < ids.length; i++) {
            area.setId(ids[i]);
            areaService.updateById(area);
        }
        return R.ok();
    }

    /**
     * 查询所有机型下拉
     *
     * @return
     */
    @RequestMapping("/getAll")
    public R getAll() {
        Map<String, Object> params = new HashMap<>();
        params.put("del_flag", "1");
        List<AreaEntity> areaEntityList = (List<AreaEntity>) areaService.listByMap(params);
        return R.ok().put("areaEntityList", areaEntityList);
    }
}
